package grammar.parser.stmt;

import grammar.parser.BlockItem;
import word.parser.WordElement;

import java.util.ArrayList;
import java.util.List;

public class Block extends Stmt {
    private final List<BlockItem> blockItemList = new ArrayList<>();

    public static Block buildBlock() {
        Block block = new Block();
        assert getAnalyzingWord().getType() == WordElement.WordType.LBRACE;
        analyzeNextWord();

        addScope();

        while (getAnalyzingWord().getType() != WordElement.WordType.RBRACE) {
            block.blockItemList.add(BlockItem.buildBlockItem());
        }
        analyzeNextWord();
        outScope();

        return block;
    }

    public List<BlockItem> getBlockItemList() {
        return blockItemList;
    }

    @Override
    public void execute() {
        addScope();
        for (int i = 0; i < blockItemList.size(); ++i) {
            BlockItem blockItem = blockItemList.get(i);

            blockItem.execute();


            if (getSemanticParser().isCallReturn() ||
                    getSemanticParser().isCallBreakOrContinue()) {
                break;
            }
        }

        outScope();
    }
}
